package com.wyz.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

@Configuration
//如果你想diy一些定制化的功能，只要写这个组件，然后将它交给springboot，springboot就会帮我们自动装配!
//扩展springmvc dispatchServlet
public class MvcConfig implements WebMvcConfigurer {
    /*//ViewResolver实现了试图解析器接口类 我们就将他交给spring boot，他会帮我们自动装配
    @Bean
    public ViewResolver MyViewResolver(){
        return new MyViewResolver();
    }

    //自定义了一个自己的试图解析器
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }*/

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //设置浏览器地址栏 localhost:888 就可以访问
        registry.addViewController("/").setViewName("/index.html");
        //设置浏览器地址栏 localhost:888/index.html就可以访问
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard");
    }

    @Bean  //自定义的国际化组件就生效了
    public LocaleResolver localeResolver(){
        return new LanguageResolver();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/index.html","/","/user/login","/css/*","/js/**","/img/**");
    }
}
